package com.baseinfinityrun;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;
import android.util.Xml;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Screen;
import com.badlogic.androidgames.framework.gl.Animation;
import com.badlogic.androidgames.framework.gl.Camera2D;
import com.badlogic.androidgames.framework.gl.SpriteBatcher;
import com.badlogic.androidgames.framework.gl.TextureRegion;
import com.badlogic.androidgames.framework.impl.GLGame;
import com.badlogic.androidgames.framework.impl.GLGraphics;
import com.badlogic.androidgames.framework.math.OverlapTester;
import com.badlogic.androidgames.framework.math.Vector2;
import com.gameObjects.BG1;
import com.gameObjects.BG2;
import com.gameObjects.Background;
import com.gameObjects.Barrier;
import com.gameObjects.GameObject;
import com.gameObjects.Player;
import com.thoughtworks.xstream.XStream;

public class GameScreen extends Screen
{
	private final float FRUSTUM_WIDTH = 4.8f;
	private final float FRUSTUM_HEIGHT = 3.2f;
	
	float animTime;
	int numberOfBG = 1;
	
	SpriteBatcher batcher;
	Camera2D camera;
	GLGraphics glGraphics;
	Player player;
	Background bgActual, lastBg;
	List<Barrier> Actualbarriers;
	InputStream stream;
	
	public GameScreen(Game game) 
	{
		super(game);
		glGraphics = ((GLGame)game).getGLGraphics();
		camera = new Camera2D(glGraphics,FRUSTUM_WIDTH,FRUSTUM_HEIGHT);		
		batcher = new SpriteBatcher(glGraphics, 10);
		player = new Player(1, 1, 0.99f, 1.32f);
		
		try
		{
			stream = game.getFileIO().readAsset("obstacle.xml");
			Actualbarriers = BarrierLoad(stream);
		}
		

		catch(IOException e)
		{
			Log.d("testes", e.getMessage());
		}
		

		lastBg = new BG1(0, FRUSTUM_HEIGHT/2, FRUSTUM_WIDTH, FRUSTUM_HEIGHT );
		bgActual = new BG1(lastBg.position.x + lastBg.size.x,lastBg.position.y, lastBg.size.x,lastBg.size.y);
	}

	@Override
	public void update(float deltaTime) 
	{
		ChangeBG();
		player.Update(deltaTime);		
		camera.position.x = player.position.x;
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		
		int len = touchEvents.size();
		for(int i = 0; i < len; i++)
		{
			TouchEvent ev = touchEvents.get(i);
			if(ev.type == TouchEvent.TOUCH_DOWN)
			{
				player.Jump();
			}
		}
	}

	@Override
	public void present(float deltaTime)
	{
		 GL10 gl = glGraphics.getGL();
	     gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	     camera.setViewportAndMatrices();
	        
	     gl.glEnable(GL10.GL_BLEND);
	     gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
	     gl.glEnable(GL10.GL_TEXTURE_2D);
	     
	     lastBg.Draw(batcher);
	     bgActual.Draw(batcher);
	     
	     for(Barrier barrier : Actualbarriers)
	     {
	    	 barrier.Draw(batcher);
	     }
	     player.Draw(batcher);   
	  }

	@Override
	public void pause() 
	{
		
	}

	@Override
	public void resume()
	{
		//Assets.Load((GLGame)game);
	}

	@Override
	public void dispose()
	{
		
	}
	
	private void ChangeBG()
	{
		if(player.position.x + player.bounds.width/4 > bgActual.position.x)
		{
			lastBg = bgActual;
			switch(numberOfBG)
			{
				case 1:
					bgActual = new BG1(lastBg.position.x + lastBg.size.x,lastBg.position.y, lastBg.size.x,lastBg.size.y);
					break;
					
				case 2:
					bgActual = new BG2(lastBg.position.x + lastBg.size.x,lastBg.position.y, lastBg.size.x,lastBg.size.y);
					break;
					
				default:
					break;
			}
		}
	}
	
	List<Barrier> BarrierLoad(InputStream stream)
	{
		List<Barrier> barriers = new ArrayList<Barrier>();
		Barrier barrier = null;
		
		String text = null, tagName = null;
		
		XmlPullParserFactory factory;
		XmlPullParser parser;
		
		try
		{
			factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			parser = factory.newPullParser(); 
			
			parser.setInput(stream, null);
						
			int evType = parser.getEventType();
			
			while(evType != XmlPullParser.END_DOCUMENT)
			{
				tagName = parser.getName();
				switch(evType)
				{
					case XmlPullParser.START_TAG:
						if(tagName.equalsIgnoreCase("barrier"))
						{
							barrier = new Barrier();
						}
						break;
					
					case XmlPullParser.TEXT:
						text = parser.getText();
						break;
						
					case XmlPullParser.END_TAG:
						
						if(tagName.equalsIgnoreCase("barrier"))
						{
							barrier.SetBarrier();
							barriers.add(barrier);
						}
						
						else if(tagName.equalsIgnoreCase("x"))
						{
							barrier.SetX(Float.parseFloat(text));
						}
						
						else if(tagName.equalsIgnoreCase("y"))
						{
							barrier.SetY(Float.parseFloat(text));
						}
						
						else if(tagName.equalsIgnoreCase("w"))
						{
							barrier.SetW(Float.parseFloat(text));
						}
						
						else if(tagName.equalsIgnoreCase("h"))
						{
							barrier.SetH(Float.parseFloat(text));
						}
						break;
						
					default:
						break;
				
				}
				evType = parser.next();
			}
		}
		
		catch(XmlPullParserException e)
		{
			Log.d("testes", "erro por: " + e.getMessage()); 
		}
		catch (IOException e) {
			Log.d("testes", "erro por: " + e.getMessage()); 
		}
		
		return barriers; 
	}

}
